package online.longzipeng.cinemasys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import online.longzipeng.cinemasys.dto.type.TypeQueryResponse;
import online.longzipeng.cinemasys.entity.Type;
import online.longzipeng.cinemasys.mapper.TypeMapper;
import online.longzipeng.cinemasys.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.longzipeng.cinemasys.vo.type.TypeQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2021-04-09
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    TypeMapper typeMapper;

    @Override
    public void add(Type type) {
        save(type);
    }

    @Override
    public void updateType(Type type) {
        updateById(type);
    }

    @Override
    public IPage<TypeQueryResponse> getListPage(TypeQueryVO queryVO) {
        Page<TypeQueryResponse> typePage = new Page<>(queryVO.getPage(),queryVO.getSize());
        QueryWrapper<TypeQueryResponse> qw = new QueryWrapper<>();
        // 判断查询条件 名称 类型级别
        qw.eq(!StringUtils.isEmpty(queryVO.getName()),"name",queryVO.getName());
        if (!StringUtils.isEmpty(queryVO.getTypeLevel())) {
            if (queryVO.getTypeLevel() == 0) {
                qw.eq("parent_id",0);
            }else {
                qw.ne("parent_id",0);
            }
        }
        // 判断排序
        if ("desc".equals(queryVO.getSort())) {
            qw.orderByDesc("id");
        }else {
            qw.orderByAsc("id");
        }
        return typeMapper.getListPage(typePage,qw);
    }

    @Override
    public List<Type> getAllLevelOneJson() {
        QueryWrapper<Type> qwType = new QueryWrapper<>();
        qwType.eq("parent_id",0);
        return list(qwType);
    }

    @Override
    public List<Type> getAllLevelTwoJson() {
        QueryWrapper<Type> qwType = new QueryWrapper<>();
        qwType.ne("parent_id",0);
        return list(qwType);
    }

    @Override
    public List<Type> getAllLevelTwoJsonByLevelOneId(Long id) {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        return list(queryWrapper);
    }
}
